package com.rt.easyjava.common.base;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Result<T> implements Serializable{
	private static final long serialVersionUID = 1L;

	private List<T> list = new ArrayList<T>();

	private T obj = null;

	private Map<String,Object> map = new HashMap<String, Object>();

	private String remark = "";

	private Boolean success = false;
	private Integer code = 0;
	private String content = "";
	private String contentOnly = "";

	//page
	private int total;
	public Result setTotal(int total) {
		this.total = total;
		return this;
	}


	/**
	 * 快捷构造对象
	 * @param os
	 * @return
	 */
	synchronized public static Result builder(Object ... os){
		Object o = "";
		if (os.length > 0) {
			o = os[0];
		}
		Result result = new Result();
		// init something
		result.getMap().put("total", 0);
		result.getMap().put("rows", new ArrayList<>());

		if (o instanceof List) {
			result.setList((List)o);
		}
		else if (o instanceof Map) {
			result.setMap((Map)o);
		}
		else if (o instanceof String) {
			result.setRemark((String)o);
		}
		else if (o instanceof Object) {
			result.setObj(o);
		}
		return result.success();
	}

	public boolean isSuccess(){
		return success;
	}

	public Result<T> success(String ... reasons){
		this.success = true;
		this.code = 1;

		if(reasons==null || reasons.length==0){
			content = "操作成功";
			contentOnly = "";
		}else{
			content = "操作成功："+reasons[0];
			contentOnly = reasons[0];
		}
		return this;
	}

	public Result<T> fault(String ... reasons){
		this.success = false;
		this.code = 0;

		if(reasons==null || reasons.length==0){
			content = "操作失败";
			contentOnly = "";
		}else{
			content = "操作失败："+reasons[0];
			contentOnly = reasons[0];
		}
		return this;
	}

	public void response(HttpServletResponse response) throws IOException {
		response.setContentType("text/json;charset=utf-8");
		Writer out = response.getWriter();
		out.write(new Gson().toJson(this));
		out.flush();
		out.close();
	}

	public Result<T> setMap(Map<String,Object> map) {
		this.map = map;
		return this;
	}
	public Result<T> setList(List<T> list) {
		this.list = list;
		return this;
	}

	public Result<T> setObj(T obj) {
		this.obj = obj;
		return this;
	}

	public Result<T> setRemark(String remark) {
		this.remark = remark;
		return this;
	}

	public List<T> getList() {
		return list;
	}

	public T getObj() {
		return obj;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public String getRemark() {
		return remark;
	}

	public String getContent() {
		return content;
	}

	public String getContentOnly() {
		return contentOnly;
	}

	public Boolean getSuccess() {
		return success;
	}

	public Integer getCode() {
		return code;
	}

	public int getTotal() {
		return total;
	}

	@Override
	public String toString(){
		// return new Gson().toJson(this);
		return JSONObject.toJSONString(this);
	}


}
